#pragma once

#include <iostream>
#include <string>
#include <ctemplate/template.h>
//#include <oj_model.hpp>
#include <vector>
#include "../commen/log.hpp"
#include "oj_model_mysql.hpp"

namespace ns_oj_view
{
    //using namespace ns_oj_model;
    using namespace ns_oj_model;
    using namespace ns_log;
    const std::string html_src = "./question_html";

    class OjView
    {
    public: 
        OjView(const std::string &src = html_src)
            : _html_src(src)
        {}

        ~OjView()
        {}
    public:
        void AllExpandHtml(const std::vector<Question> &questions, std::string* html)
        {
            //题目的编号 题目的标题 题目的难度
            //1.选择html
            std::string src = _html_src + "/all_questions.html";
            //2.形成数据字典
            ctemplate::TemplateDictionary root("all_questions");
            for (auto& q: questions)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number", std::to_string(q.number));
                sub->SetValue("title", q.title);
                sub->SetValue("star", q.star);
            }

            //3.导入网页模板
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src, ctemplate::DO_NOT_STRIP);
            
            //4.形成渲染后的网页
            tpl->Expand(html, &root);
        }

        void OneExpandHtml(const Question &question, std::string* html)
        {
            // 1.选择html
            std::string src = _html_src + "/one_question.html";
            //2.形成数据字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number", std::to_string(question.number));
            root.SetValue("title", question.title);
            root.SetValue("star", question.star);
            root.SetValue("desc", question.desc);
            root.SetValue("header", question.header);
            //3.导入网页模板
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src, ctemplate::DO_NOT_STRIP);
            //4.形成渲染后的网页
            tpl->Expand(html, &root);
            LogMessage(Debug, "形成渲染后的网页成功\n");
        }

    private:
        std::string _html_src;
    };
}